#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007 by Kai Blin
#
# Plunger is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
"""Plunger is a tool to make the WorldForge media pipeline flow more smoothly.

To do so, it will convert 3D assets from the Collada format to MD3, Cal3D and
Ogre.

Plunger is copyright (c) 2007 by Kai Blin  <kai.blin@gmail.com>.
Plunger is covered under the GNU GPL, see COPYING for details.
"""
import getopt
import sys
import os

try:
    from plunger import core
except ImportError:
    import core

VERSION = "0.1.0"

def usage():
    """Print the usage.
    """
    print """Usage: plunger [options] <input name> <output name>
Possible options are:
--help              -h          Display this help text.
--debug             -d          Print debugging output.
--in=<format>       -i <format> Input file is in <format>. [default: collada]
--out=<format>      -o <format> Convert to <format>.       [default: info]
--display-formats   -D          List supported input/output formats.
--version           -V          Display plunger version.
"""

def displayFormats():
    """Display import and export formats currently supported by plunger
    """
    print "Supported import formats: %s" % ", ".join(core.getImportFormats())
    print "Supported export formats: %s" % ", ".join(core.getExportFormats())

def main(argv):
    input_format = None
    output_format = None
    _debug = 0

    try:
        opts, args = getopt.getopt(argv, "hdi:o:DV",
            ["help","debug","in=","out=","display-formats", "version"])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    core.loadPlugins()

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt in ("-d", "--debug"):
            _debug = 1
        elif opt in ("-i", "--in"):
            input_format = arg
            if not input_format in core.getImportFormats():
                print "Error, format '%s' is not supported." % input_format
                sys.exit(10)
        elif opt in ("-o", "--out"):
            output_format = arg
            if not output_format in core.getExportFormats():
                print "Error, format '%s' is not supported." % output_format
                sys.exit(11)
        elif opt in ("-D", "--display-formats"):
            displayFormats()
            sys.exit(0)
        elif opt in ("-V", "--version"):
            print "Plunger %s" % VERSION
            sys.exit(0)

    if len(args) < 2:
        usage()
        sys.exit(23)

    if _debug:
        print "Input format  = %s" % input_format
        print "Output format = %s" % output_format

    if not input_format: input_format = "collada"
    if not output_format: output_format = "info"

    core.convert(args[0], input_format, args[1], output_format)

if __name__ == "__main__":
    main(sys.argv[1:])
